package com.ziyun.erp.modules.push.service.impl;

import com.service.utils.push.model.MessageDetail;
import com.service.utils.push.model.PulishSoundType;
import com.service.utils.push.umeng.core.untils.UMengPulishUtils;
import com.utility.New;
import com.ziyun.erp.modules.supp.entity.PopSysUserEntity;
import com.ziyun.erp.modules.supp.service.PopSysUserService;
import io.renren.common.exception.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.push.dao.ErpGoodsPushDao;
import com.ziyun.erp.modules.push.entity.ErpGoodsPushEntity;
import com.ziyun.erp.modules.push.service.ErpGoodsPushService;


@Slf4j
@Service("erpGoodsPushService")
public class ErpGoodsPushServiceImpl extends ServiceImpl<ErpGoodsPushDao, ErpGoodsPushEntity> implements ErpGoodsPushService {

    @Autowired
    private PopSysUserService popSysUserService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<ErpGoodsPushEntity> page = this.selectPage(
                new Query<ErpGoodsPushEntity>(params).getPage(),
                new EntityWrapper<ErpGoodsPushEntity>().orderBy("create_time", false)
        );

        return new PageUtils(page);
    }

    @Override
    public void saveInfo(ErpGoodsPushEntity erpGoodsPush) {
        log.info("================= erp goods push start==================");
        Integer pushType = erpGoodsPush.getPushType();
        String pushId = erpGoodsPush.getPushId();
        Map<String, String> extraFieldMap = New.hashMap();
        extraFieldMap.put("pushType", pushType.toString());
        extraFieldMap.put("pushId", pushId);

        boolean send = false;
        MessageDetail messageDetail = new MessageDetail();
        messageDetail.setTitle(erpGoodsPush.getPushTitle());
        messageDetail.setBody(erpGoodsPush.getPushContent());
        messageDetail.setPulishSoundType(PulishSoundType.none);
        messageDetail.setExtraFieldMap(extraFieldMap);
        Integer pushWay = erpGoodsPush.getPushWay();
        try {
            if (pushWay == 0) {//广播
                send = UMengPulishUtils.sendBroadCast(messageDetail);
            }
            if (pushWay == 1) {//独播

                messageDetail.setDeviceTokens(erpGoodsPush.getPushTarget());
                send = UMengPulishUtils.sendUnit(messageDetail);
            }
            if (pushWay == 2) {
                //组播
                messageDetail.setFilterList(Arrays.asList(erpGoodsPush.getPushTarget()));
                send = UMengPulishUtils.sendGroupCast(messageDetail);
            }

        } catch (Exception e) {
            e.printStackTrace();
            log.error("================= erp goods push exception {}", e.getMessage());
        }

        if (!send) {
            throw new RRException("消息推送失败，请联系后台管理员");
        }
        this.insert(erpGoodsPush);
        log.info("================= erp goods push start==================");
    }

    @Override
    public List<PopSysUserEntity> getPopUserList(Map<String, Object> params) {
        return popSysUserService.getUserList(params);
    }

}
